package edu.zzuli.assistant.api.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import edu.zzuli.assistant.bean.Comment;
import edu.zzuli.assistant.bean.News;
import edu.zzuli.assistant.bean.User;
import edu.zzuli.assistant.service.CommentService;
import edu.zzuli.assistant.service.impl.CommentServiceImpl;
import edu.zzuli.assistant.until.StrUtil;

public class CommentAction extends ActionSupport implements ModelDriven ,SessionAware{
	
	private CommentService commentService = new CommentServiceImpl();
	private Map<String, Object> session;
	private List<Comment> commentList;
	private Comment comment;
	private int page = 1;
	private final int  PAGE_NUM = 10;
	
	private String[] fields = {"id","authorName","authorId","replyId","replyName","content","postTime"};
	private String[] fieldNews = {"id","authorName","authorId","replyId","replyName","content","postTime","news"};
	private String[] clazzFields ={"id","title"};
	
	private User user;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public Comment getComment() {
		return comment;
	}
	public List<Comment> getCommentList() {
		return commentList;
	}

	public int getPage() {
		return page;
	}
	public Map<String, Object> getSession() {
		return session;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}
	public void setCommentList(List<Comment> commentList) {
		this.commentList = commentList;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if(comment ==null){
			this.comment = new Comment();
		}
		return comment;
	}
	
	
	//comment_replyme.acion 评论我的
	public  void replyme(){
		
		if(session.get("userId")!=null){
			String userId= session.get("userId").toString();
			commentList =  commentService.getReplyCommentList(userId,page,PAGE_NUM);
			StrUtil.sendJson(Comment.class, fieldNews , News.class ,clazzFields,commentList);
		}else{
			StrUtil.sendMsg(LOGIN);
		}
		
	}
	//comment_myreply.acion 我的评论
	public  void myreply(){
		
		if(session.get("userId")!=null){
			String userId= session.get("userId").toString();
			commentList =  commentService.getUserCommentList(userId, page, PAGE_NUM);
			StrUtil.sendJson(Comment.class, fieldNews , News.class ,clazzFields,commentList);
		}else{
			StrUtil.sendMsg(LOGIN);
		}
		
	}
	//comment_add.acion 添加一条评论
	public void add(){
		if(session.get("userId")!=null){
			String userId= session.get("userId").toString();
			comment.setAuthorId(userId);
			comment.setAuthorName(session.get("userName").toString());
			comment.setPostTime(new Date());
			boolean result = commentService.addComment(comment);
			if(result){
				StrUtil.sendMsg(SUCCESS);
			}
			else{
				StrUtil.sendMsg("fail");
			}
		}else{
			StrUtil.sendMsg(LOGIN);
		}
		
	}
	
	public void list(){
		if(comment.getNews()!=null){
			System.out.println(comment.getNews().getId());
			commentList = commentService.getNewsCommentList(comment.getNews().getId(), page, PAGE_NUM);
			StrUtil.sendJson(Comment.class, fields , commentList);
		}
	}
}
